﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageGuest
{
    public partial class cListProductOfCategory : System.Web.UI.Page
    {
        CCongTy _cbtm = new CCongTy();
        CDanhMuc _cdm = new CDanhMuc();
        DanhMuc danhmuc = new DanhMuc();
        List<DanhMuc> listdm = new List<DanhMuc>();
        CThongTinCaNhan _cttcn = new CThongTinCaNhan();
        private static int _currentPage = 1;
        private static int _currentCountProduct = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
            pnEmtry.Visible = false;
            if (!IsPostBack)
            {
                Filter();
            }
        }

        private void LoadGV(int iddanhmuc)
        {
            txtTrangHienTai.Text = _currentPage.ToString();
            int size = gvListProducts.PageSize;
            int begin = (_currentPage - 1) * size;
            List<CongTy> list = _cbtm.GetListOfIDCatalogy(listdm, iddanhmuc, begin, size);
            if (list.Count > 0)
            {
                gvListProducts.DataSource = list;
                gvListProducts.DataBind();
                pnEmtry.Visible = false;
                lbGridViewEmtry.Text = "";
                pnPhanTrang.Visible = true;
            }
            else
            {

                pnEmtry.Visible = true;
                pnEmtry.CssClass = "InLeft info message";
                pnPhanTrang.Visible = false;
                gvListProducts.DataSource = null;
                gvListProducts.DataBind();
                lbGridViewEmtry.Text = "There are no data.";

            }
        }

        private void Filter()
        {
            _currentPage = 1;
            LoadTotalPageNumber(danhmuc.IDDanhMuc);
            LoadGV(danhmuc.IDDanhMuc);
        }

        protected void LoadTotalPageNumber(int status)
        {
            int soLuongTrang = _cbtm.slTrangDuLieuOfIDCatalogy(gvListProducts.PageSize, _currentCountProduct);
            lbTrang.Text = soLuongTrang.ToString();
        }

        private void HyperLinkQuick(int id)
        {
            DanhMuc dm = new DanhMuc();
            dm = _cdm.GetByID(id);
            if (dm.IDDanhMucCha != null)
            {
                DanhMuc dmparent = new DanhMuc();
                dmparent = _cdm.GetByID((int)dm.IDDanhMucCha);
                HyperLink hl = new HyperLink();
                hl.ID = "hl" + id.ToString();
                if (dmparent.IDDanhMucCha != null)
                {
                    hl.NavigateUrl = "~/PageGuest/cListProductOfCategory.aspx?pid=" + dm.IDDanhMucCha;
                }
                else
                {
                    hl.NavigateUrl = "~/PageGuest/cListOfCategoryLevel1.aspx?pid=" + dm.IDDanhMucCha;
                }
                hl.Text = dmparent.TenDanhMuc;
                HyperLinkQuick((int)dm.IDDanhMucCha);
                linkQuick.Controls.Add(hl);
                Label lb = new Label();
                lb.Text = " > ";
                lb.ID = "lbb" + dm.IDDanhMuc.ToString();
                linkQuick.Controls.Add(lb);
            }
        }

        private void LoadDuLieu()
        {
            if (Request.Params["pid"] != null)
            {
                try
                {
                    int id = Convert.ToInt32(Request.Params["pid"].ToString());
                    danhmuc = _cdm.GetByID(id);

                    HyperLinkQuick(id);
                    Label lb = new Label();
                    lb.ID = "lb" + id.ToString();
                    _currentCountProduct = _cdm.GetQuantityOfCatDistinctCompany(id);
                    lb.Text = danhmuc.TenDanhMuc + " ( " + _currentCountProduct.ToString() + " Buy Offers Found)";
                    linkQuick.Controls.Add(lb);
                    //load danh muc con neu co

                    listdm = _cdm.GetChildrenOfCategory(danhmuc.IDDanhMuc, 1);
                    dlCatalogyChildren.DataSource = listdm;
                    dlCatalogyChildren.DataBind();

                }
                catch
                {
                    Response.Redirect("~/PageGuest/PageError.aspx");
                }
            }
            else
            {
                Response.Redirect("~/PageGuest/PageError.aspx");
            }
        }

        public string LoadTen()
        {
            return danhmuc.TenDanhMuc;
        }

        public string LoadCount()
        {
            return "( " + _currentCountProduct.ToString() + " Buy Offers )";
        }

        public string LoadTotalPage()
        {
            return " " + lbTrang.Text + " ";
        }

        public string LoadCurrentPage()
        {
            return " " + _currentPage.ToString() + " ";
        }

        public string LoadChildrenCount(object obj)
        {
            int ID = Convert.ToInt32(obj.ToString());
            DanhMuc dm = _cdm.GetByID(ID);
            int count = _cdm.GetQuantityOfCatDistinctCompany(ID);
            return count.ToString();
        }

        public string LoadCountry(object obj) {
            int id = Convert.ToInt32(obj.ToString());

            ThongTinTaiKhoan tk = _cttcn.GetAcountOfCompany(id);

            return tk.QuocGia.TenQuocGia;
        }

        public string LoadGioiThieuCongTy(object obj)
        {
            if (obj.ToString().Length > 300) {
                return obj.ToString().Substring(0, 300) + "...";
            }
            return obj.ToString();
        }

        protected void lbtnBegin_Click(object sender, EventArgs e)
        {
            _currentPage = 1;
            LoadGV(danhmuc.IDDanhMuc);
        }

        protected void lbtnPre_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                LoadGV(danhmuc.IDDanhMuc);
            }
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            if (_currentPage < int.Parse(lbTrang.Text))
            {
                _currentPage++;
                LoadGV(danhmuc.IDDanhMuc);
            }
        }

        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            _currentPage = int.Parse(lbTrang.Text);
            LoadGV(danhmuc.IDDanhMuc);
        }

        protected void lbtnToi_Click(object sender, EventArgs e)
        {
            int goToPage = Convert.ToInt32(txtTrangHienTai.Text);
            int soLuongTrang = int.Parse(lbTrang.Text);
            if (goToPage > soLuongTrang)
            {
                _currentPage = soLuongTrang;
            }
            else if (goToPage <= soLuongTrang)
            {
                _currentPage = goToPage;
            }
            LoadGV(danhmuc.IDDanhMuc);
        }
    }
}